define(["Vue", "text!../../../views/study1/index.html", "underscore", "./cell"], (Vue, template, _, Cell) ->
  return Vue.component("v-study1", {
    template: template,
    data: () -> { 
      tanCount: 40, # 障碍数量
      cells: [],
      colors: [
        "aliceblue", # 空白通道
        "tan", # 障碍
        "chartreuse", # 玩家
        "skyblue" # 范围
      ],
      openList: [],
      closeList: [],
      user: undefined,
      target: undefined,
      interval: undefined
    },
    mounted: () -> (
      console.log("dom树加载完成， 开始预处理")
      tanCount = this.tanCount
      this.cells = _.map([0..15], (i) => 
        return _.map([0..15], (j) => 
          # 第一个格子 默认为 玩家
          if i == 0 && j == 0
            return new Cell(i, j, 2)
          # 随机数 取 0 时， 为障碍， 最多 40个
          else if _.random(0, 5) == 0 && tanCount > 0
            tanCount--
            return new Cell(i, j, 1)
          # 剩余均为空白通道
          return new Cell(i, j, 0)
        )
      )
      this.resetData(true)
    ),
    methods: {
      # 判断该节点是否关闭
      isClosed: (cell) -> (
        item = _.find(this.closeList, (item) => item.row == cell.row && item.col == cell.col)
        if item
          return true
        return false
      )
      # 获取节点 周边 节点
      getRound: (cell) -> (
        result = []
        for i in [cell.row - 1, cell.row, cell.row + 1]
          for j in [cell.col - 1, cell.col, cell.col + 1]
            if i > 0 && j > 0 && !(i == cell.row && j == cell.col)
              if !this.isClosed(this.cells[i][j])
                result.push(this.cells[i][j])
        return result
      ),
      # 获取玩家路径
      getPath: () -> (
        console.log(this.openList, this.closeList)
        _.each(this.openList, (item) =>
          # item.setCome(this.user)
          # item.setGo(this.target)
          result = this.getRound(item)
          console.log(item, result)
        )
        # if this.interval
        #   window.clearInterval(this.interval)
        
        # result = []
        
        # row = this.user.row
        # col = this.user.col
        # for i in [(row - 1 - padding)..(row + 1 + padding)]
        #   for j in [(col - 1 - padding)..(col + 1 - padding)]
        #     if i >= 0 && j >= 0 && !(i == row && j == col)
        #       result.push(this.cells[i][j])
              
        # count = result.length
        
        # return if result.length == 0

        # this.interval = window.setInterval(() =>
        #   count--
        #   if count >= 0
        #     console.log(result[count])
        #   else
        #     this.getRound(padding + 1)
        # , 1000)
      ),
      # 点击目标地点
      toTarget: (cell) -> (
        if this.interval
          return alert("玩家 正在移动中 ！")
        else if cell.type != 0
          return alert("玩家本体 / 障碍物 不可点击！！")
        this.target = cell
        this.resetData(false)
        this.getPath()
      ),
      # 地图重新初始化
      refresh: () -> (
        # 更新 cells
        tanCount = this.tanCount
        _.each(this.cells, (row) =>
          _.each(row, (cell) =>
            type = 0
            if cell.row == 0 && cell.col == 0
              type = 2
            else if _.random(0, 5) == 0 && tanCount > 0
              tanCount--
              type = 1
            cell.reset(type)
          )
        )
        this.resetData(true)
      ), 
      # 更新 openList 和 closeList
      resetData: (refresh) -> (
        this.user = if refresh then this.cells[0][0] else this.user
        this.openList = [this.user]
        this.closeList = []
        _.each(this.cells, (row) =>
          this.closeList = this.closeList.concat(_.filter(row, (col) -> col.type == 1))
        )
      )
    }
  });
)